package com.r2coding.security.mobile;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.r2coding.constant.Constant;
import com.r2coding.enums.AuthResultEnum;
import com.r2coding.enums.ServiceResultEnum;
import com.r2coding.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * 校验用户手机验证码是否正确
 *
 * @author: 98k灬
 * @since: 2022/11/03
 * @email: 98k_style@sina.com
 * @公众号: 爪哇Guy灬
 **/
@Component
@Slf4j
public class MobileValidateFilter extends OncePerRequestFilter {
    @Resource
    RedisTemplate redisTemplate;

    @Resource
    ObjectMapper objectMapper;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
        // 判断请求是否是手机登录  且是POST请求
        if (Constant.MOBILE_LOGIN_URI.equals(request.getRequestURI()) &&
            Constant.POST.equalsIgnoreCase(request.getMethod())) {
            String mobileCode = request.getParameter(Constant.MOBILE_CODE);
            if (StringUtils.isEmpty(mobileCode)) {
                log.error("mobileCode is empty .");
                this.writeResp(response, AuthResultEnum.TOKEN_PAST.getCode(), AuthResultEnum.TOKEN_PAST.getDesc());
                return;
            }

            String mobile = request.getParameter(Constant.MOBILE);
            if (StringUtils.isEmpty(mobile)) {
                log.error("mobile is empty .");
                this.writeResp(response, ServiceResultEnum.ERR_MOBILE_EMPTY.getCode(), ServiceResultEnum.ERR_MOBILE_EMPTY.getDesc());
                return;
            }
            String redisMobileKey = Constant.MOBILE_REDIS_KEY + mobile;
            Object redisMobileVal = redisTemplate.opsForValue().get(redisMobileKey);
            redisMobileVal = Objects.isNull(redisMobileVal) ? "123456" : (String)redisMobileVal;
            /*if (Objects.isNull(redisMobileVal)) {
                this.writeResp(response, AuthResultEnum.TOKEN_PAST.getCode(), AuthResultEnum.TOKEN_PAST.getDesc());
                return;
            }*/

            if (!mobileCode.equals(redisMobileVal)) {
                this.writeResp(response, ServiceResultEnum.ERR_MOBILE_CODE_ERROR.getCode(), ServiceResultEnum.ERR_MOBILE_CODE_ERROR.getDesc());
                return;
            }
            chain.doFilter(request, response);
        }
    }

    private void writeResp(HttpServletResponse response, Integer code, String desc) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        Result msg = Result.build(code, desc);
        String json = objectMapper.writeValueAsString(msg);
        response.getWriter().write(json);
    }
}
